home *** CD-ROM | disk | FTP | other *** search
/ C/C++ Users Group Library 1996 July / C-C++ Users Group Library July 1996.iso / listings / v_13_11 / phillip2 / skeleton.c < prev    next >
C/C++ Source or Header  |  1993-05-14  |  39KB  |  1,433 lines

  1.  
  2.        /**********************************************
  3.        *
  4.        *       file d:\cips\skeleton.c
  5.        *
  6.        *       Functions: This file contains
  7.        *          thinning
  8.        *          can_thin
  9.        *          dilate_not_join
  10.        *          can_dilate
  11.        *          little_label_and_check
  12.        *          special_closing
  13.        *          special_opening
  14.        *          edm
  15.        *          distanc_8
  16.        *          mat
  17.        *          mat_d
  18.        *
  19.        *       Purpose:
  20.        *          These functions thin objects in
  21.        *          an image, dilate objects, and
  22.        *          perform opening and closing
  23.        *          without removing or joining
  24.        *          objects.
  25.        *
  26.        *       External Calls:
  27.        *          wtiff.c - round_off_image_size
  28.        *                    create_file_if_needed
  29.        *                    write_array_into_tiff_image
  30.        *          tiff.c - read_tiff_header
  31.        *          rtiff.c - read_tiff_image
  32.        *          numcvrt.c - get_integer
  33.        *
  34.        *
  35.        *       Modifications:
  36.        *          7 March 1993 - created
  37.        *
  38.        ************************************************/
  39.  
  40. #include "cips.h"
  41.  
  42.  
  43.  
  44.  
  45.  
  46.      /*******************************************
  47.      *
  48.      *   thinning(...
  49.      *
  50.      *   Use a variation of the grass fire
  51.      *   wave front approach.
  52.      *
  53.      *   Raster scan the image left to right
  54.      *   and examine and thin the left edge pixels
  55.      *   (a 0 to value transition).  Process them
  56.      *   normally and "save" the result.  Next,
  57.      *   raster scan the image right to left and
  58.      *   save.  Raster scan top to bottom and save.
  59.      *   Raster scan bottom to top and save.
  60.      *
  61.      *   That is one complete pass.
  62.      *
  63.      *   Keep track of pixels thinned for a
  64.      *   pass and quit when you make a complete
  65.      *   pass without thinning any pixels.
  66.      *
  67.      *******************************************/
  68.  
  69.  
  70. thinning(in_name, out_name, the_image, out_image,
  71.          il, ie, ll, le, value, threshold, once_only)
  72.    char   in_name[], out_name[];
  73.    int    il, ie, ll, le, once_only;
  74.    short  the_image[ROWS][COLS],
  75.           out_image[ROWS][COLS],
  76.           threshold, value;
  77. {
  78.    int    a, b, big_count, count, i, j, k,
  79.           not_finished;
  80.  
  81.    create_file_if_needed(in_name, out_name, out_image);
  82.  
  83.    read_tiff_image(in_name, the_image, il, ie, ll, le);
  84.  
  85.  
  86.    for(i=0; i<ROWS; i++)
  87.       for(j=0; j<COLS; j++)
  88.          out_image[i][j] = the_image[i][j];
  89.  
  90.    not_finished = 1;
  91.    while(not_finished){
  92.  
  93.       if(once_only == 1)
  94.         not_finished = 0;
  95.       big_count = 0;
  96.  
  97.          /***************************
  98.          *
  99.          *   Scan left to right
  100.          *   Look for 0-value transition
  101.          *
  102.          ****************************/
  103.  
  104.       printf("\n");
  105.       for(i=1; i<ROWS-1; i++){
  106.          if( (i%10) == 0) printf("%3d", i);
  107.          for(j=1; j<COLS-1; j++){
  108.             if(the_image[i][j-1] == 0   &&
  109.                the_image[i][j]   == value){
  110.                count = 0;
  111.                for(a=-1; a<=1; a++){
  112.                    for(b=-1; b<=1; b++){
  113.                          if(the_image[i+a][j+b] == 0)
  114.                             count++;
  115.                    }  /*  ends loop over b */
  116.                }  /* ends loop over a */
  117.                if(count > threshold){
  118.                   if(can_thin(the_image, i, j, value)){
  119.                      out_image[i][j] = 0;
  120.                      big_count++;
  121.                   }  /* ends if can_thin */
  122.                }  /* ends if count > threshold */
  123.             }  /* ends if the_image == value */
  124.          }  /* ends loop over j */
  125.       }  /* ends loop over i */
  126.  
  127.          /**************************************
  128.          *
  129.          *   Copy the output back to the input.
  130.          *
  131.          **************************************/
  132.  
  133.       for(i=0; i<ROWS; i++)
  134.          for(j=0; j<COLS; j++)
  135.             the_image[i][j] = out_image[i][j];
  136.  
  137.  
  138.          /***************************
  139.          *
  140.          *   Scan right to left
  141.          *   Do this by scanning left
  142.          *   to right and look for
  143.          *   value-0 transition.
  144.          *
  145.          ****************************/
  146.  
  147.       printf("\n");
  148.       for(i=1; i<ROWS-1; i++){
  149.          if( (i%10) == 0) printf("%3d", i);
  150.          for(j=1; j<COLS-1; j++){
  151.             if(the_image[i][j+1] == 0   &&
  152.                the_image[i][j]   == value){
  153.                count = 0;
  154.                for(a=-1; a<=1; a++){
  155.                    for(b=-1; b<=1; b++){
  156.                          if(the_image[i+a][j+b] == 0)
  157.                             count++;
  158.                    }  /*  ends loop over b */
  159.                }  /* ends loop over a */
  160.                if(count > threshold){
  161.                   if(can_thin(the_image, i, j, value)){
  162.                      out_image[i][j] = 0;
  163.                      big_count++;
  164.                   }  /* ends if can_thin */
  165.                }  /* ends if count > threshold */
  166.             }  /* ends if the_image == value */
  167.          }  /* ends loop over j */
  168.       }  /* ends loop over i */
  169.  
  170.          /**************************************
  171.          *
  172.          *   Copy the output back to the input.
  173.          *
  174.          **************************************/
  175.  
  176.       for(i=0; i<ROWS; i++)
  177.          for(j=0; j<COLS; j++)
  178.             the_image[i][j] = out_image[i][j];
  179.  
  180.  
  181.          /***************************
  182.          *
  183.          *   Scan top to bottom
  184.          *   Look for 0-value transition
  185.          *
  186.          ****************************/
  187.  
  188.       printf("\n");
  189.       for(j=1; j<COLS-1; j++){
  190.          if( (j%10) == 0) printf("%3d", j);
  191.          for(i=1; i<ROWS-1; i++){
  192.             if(the_image[i-1][j] == 0   &&
  193.                the_image[i][j]   == value){
  194.                count = 0;
  195.                for(a=-1; a<=1; a++){
  196.                    for(b=-1; b<=1; b++){
  197.                          if(the_image[i+a][j+b] == 0)
  198.                             count++;
  199.                    }  /*  ends loop over b */
  200.                }  /* ends loop over a */
  201.                if(count > threshold){
  202.                   if(can_thin(the_image, i, j, value)){
  203.                      out_image[i][j] = 0;
  204.                      big_count++;
  205.                   }  /* ends if can_thin */
  206.                }  /* ends if count > threshold */
  207.             }  /* ends if the_image == value */
  208.          }  /* ends loop over i */
  209.       }  /* ends loop over j */
  210.  
  211.          /**************************************
  212.          *
  213.          *   Copy the output back to the input.
  214.          *
  215.          **************************************/
  216.  
  217.       for(i=0; i<ROWS; i++)
  218.          for(j=0; j<COLS; j++)
  219.             the_image[i][j] = out_image[i][j];
  220.  
  221.  
  222.  
  223.          /***************************
  224.          *
  225.          *   Scan bottom to top
  226.          *   Do this by scanning top
  227.          *   to bottom and look for
  228.          *   value-0 transition.
  229.          *
  230.          ****************************/
  231.  
  232.       printf("\n");
  233.       for(j=1; j<COLS-1; j++){
  234.          if( (j%10) == 0) printf("%3d", j);
  235.          for(i=1; i<ROWS-1; i++){
  236.             if(the_image[i+1][j] == 0   &&
  237.                the_image[i][j]   == value){
  238.                count = 0;
  239.                for(a=-1; a<=1; a++){
  240.                    for(b=-1; b<=1; b++){
  241.                          if(the_image[i+a][j+b] == 0)
  242.                             count++;
  243.                    }  /*  ends loop over b */
  244.                }  /* ends loop over a */
  245.                if(count > threshold){
  246.                   if(can_thin(the_image, i, j, value)){
  247.                      out_image[i][j] = 0;
  248.                      big_count++;
  249.                   }  /* ends if can_thin */
  250.                }  /* ends if count > threshold */
  251.             }  /* ends if the_image == value */
  252.          }  /* ends loop over i */
  253.       }  /* ends loop over j */
  254.  
  255.          /**************************************
  256.          *
  257.          *   Copy the output back to the input.
  258.